\documentclass{article}

\usepackage{ctex}
\usepackage{geometry}
\geometry{a4paper, scale=0.85} % 设置为 A4 纸，边距为 1 英寸
\usepackage{graphicx} % 插入图片
\graphicspath{{D:/文章中枢/课程讲义/模拟电子技术-基于Multisim实现/Image}} % 图片路径
\usepackage{subcaption} 
\usepackage{amsmath} % 数学公式
\usepackage{fancyhdr} % 页眉页脚
\usepackage{circuitikz} % 电路图
\usepackage{tikz} % 绘图
\usepackage{float} % 图片浮动
\usepackage{listings} % 代码高亮
\usepackage{color} % 颜色
\usepackage{hyperref} % 超链接
\usepackage{siunitx} % SI单位 
\usepackage{comment}
\usepackage{sectsty}

\sectionfont{\centering}
\subsectionfont{\centering}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.9,0.95,1.0} % 修改为淡蓝色背景

\lstset{
    backgroundcolor=\color{backcolour},   % 背景色
    commentstyle=\color{codegreen},        % 注释样式
    keywordstyle=\color{magenta},          % 关键字样式
    numberstyle=\tiny\color{codegray},     % 行号样式
    stringstyle=\color{codepurple},        % 字符串样式
    basicstyle=\ttfamily\small,           % 基本字体样式
    breakatwhitespace=false,               % 仅在空白处换行
    breaklines=true,                       % 自动换行
    captionpos=b,                          % 标题位置(b底部)
    keepspaces=true,                       % 保持空格
    numbers=left,                          % 行号位置
    numbersep=5pt,                         % 行号与代码的距离
    showspaces=false,                      % 显示空格
    showstringspaces=false,                % 显示字符串中的空格
    showtabs=false,                        % 显示制表符
    tabsize=2,                             % 制表符宽度
    frame=single,                          % 边框样式
    rulecolor=\color{black},              % 边框颜色
}

\lstset{
    backgroundcolor=\color{backcolour},   % 背景色
    commentstyle=\color{codegreen},        % 注释样式
    keywordstyle=\color{magenta},          % 关键字样式
    numberstyle=	iny\color{codegray},     % 行号样式
    stringstyle=\color{codepurple},        % 字符串样式
    basicstyle=	tfamily\small,           % 基本字体样式
    breakatwhitespace=false,               % 仅在空白处换行
    breaklines=true,                       % 自动换行
    captionpos=b,                          % 标题位置(b底部)
    keepspaces=true,                       % 保持空格
    numbers=left,                          % 行号位置
    numbersep=5pt,                         % 行号与代码的距离
    showspaces=false,                      % 显示空格
    showstringspaces=false,                % 显示字符串中的空格
    showtabs=false,                        % 显示制表符
    tabsize=2,                             % 制表符宽度
    frame=single,                          % 边框样式
    rulecolor=\color{black},              % 边框颜色
}

% 添加汇编语言支持
\lstdefinelanguage{assembly}{
  morekeywords={MOV, ADD, SUB, MUL, DIV, INC, DEC, PUSH, POP, CALL, RET, JMP, JZ, JNZ, JC, JNC, CMP, AND, OR, XOR, NOT, LOOP, LEA}, % 汇编指令
  morecomment=[l];, % 行注释符号
  morecomment=[s]{/*}{*/}, % 块注释符号
  morestring=[b]", % 字符串定义
  sensitive=true % 区分大小写
}

\begin{document}
    下图是计算机结构的抽象图：
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}[scale=0.8]
            % 定义节点样式
            \tikzset{
            mainblock/.style={draw, rectangle, minimum width=3cm, minimum height=1.5cm, text centered, fill=blue!10},
            subblock/.style={draw, rectangle, minimum width=2cm, minimum height=1cm, text centered, fill=blue!5},
            component/.style={draw, rectangle, minimum width=1.5cm, minimum height=0.8cm, text centered},
            conn/.style={->, >=stealth, thick}
            }
            % 主节点
            \node[mainblock] (system) at (0, 0) {计算机系统};
            % 子节点 - 处理器
            \node[subblock] (processor) at (-6, -2.5) {处理器};
            \node[component] (alu) at (-7.5, -4.5) {ALU};
            \node[component] (cu) at (-4.5, -4.5) {控制器};
            \node[component] (reg) at (-6, -6) {寄存器};
            % 子节点 - 存储系统
            \node[subblock] (memory) at (0, -2.5) {存储系统};
            \node[component] (ram) at (-1.5, -4.5) {RAM};
            \node[component] (rom) at (1.5, -4.5) {ROM};
            \node[component] (cache) at (0, -6) {Cache};
            % 子节点 - I/O系统
            \node[subblock] (io) at (6, -2.5) {I/O系统};
            \node[component] (input) at (4.5, -4.5) {输入设备};
            \node[component] (output) at (7.5, -4.5) {输出设备};
            \node[component] (storage) at (6, -6) {外存};
            % 总线
            \node[draw, thick, rectangle, minimum width=10cm, minimum height=0.5cm, fill=yellow!20] (bus) at (0, -7.5) {系统总线};
            % 连线
            \draw[conn] (system) -- (processor);
            \draw[conn] (system) -- (memory);
            \draw[conn] (system) -- (io);
            
            \draw[conn] (processor) -- (alu);
            \draw[conn] (processor) -- (cu);
            \draw[conn] (processor) -- (reg);
            
            \draw[conn] (memory) -- (ram);
            \draw[conn] (memory) -- (rom);
            \draw[conn] (memory) -- (cache);
            
            \draw[conn] (io) -- (input);
            \draw[conn] (io) -- (output);
            \draw[conn] (io) -- (storage);

            \draw[conn] (reg) -- (bus);
            \draw[conn] (cache) -- (bus);
            \draw[conn] (storage) -- (bus);
        \end{tikzpicture}
        \caption{计算机系统详细结构框图}
        \label{fig:computer_system}
    \end{figure}


    计算机内存模型：
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}[scale=1.2, >=latex]
            % CPU模块
            \node[rectangle, draw, thick, minimum width=3cm, minimum height=2cm, fill=blue!10] (cpu) at (-4, 0) {\textbf{CPU}};
            % 内存模块
            \node[rectangle, draw, thick, minimum width=4cm, minimum height=9cm, fill=green!10] (memory) at (2, 0) {};
            \node[font=\bfseries] at (memory.north) [above=0.2cm] {内存模块};
            % 内存内部结构
            \node[rectangle, draw, thin, minimum width=3.5cm, minimum height=0.8cm] at (2, 3.2) {\footnotesize 内存控制器};
            \foreach \i in {1,2,...,8} {
                \node[rectangle, draw, thin, minimum width=3.5cm, minimum height=0.7cm] at (2, 3.2 - 0.8*\i) {\footnotesize 内存单元 (地址: 0x00\i)};
            }
            % 地址总线
            \foreach \i in {1,2, ...,8} {
                \draw[thin, -latex] (-1, 3.2 - 0.8*\i) -- ++(1.7, 0) node[midway, above] {\tiny 地址线};
            }
            \node[rectangle, draw, thick, minimum width=0.4cm, minimum height=0.4cm, fill=red!10] at (-1.15, 0) {};
            \draw[thick] (-1, 0) -- ++(-1.75, 0) node[midway, above] {\normalsize 地址总线};
            \draw[thick] (-1, 2.4) -- ++(0, -5.6);
            % 数据总线
            \foreach \i in {1,2, ...,8} {
                \draw[thin, latex-latex] (0.45+0.35*\i, -3.5) -- ++(0, -1) node[midway, yshift=3pt, sloped] {\tiny 数据线};
            }
            \draw[thick] (3.25, -4.5) -- ++(-7, 0) -- ++(0, 3.65) node[midway, yshift=5pt, sloped] {\normalsize 数据总线};
            % 控制总线
            \draw[thick, latex-] (0.7, 3.2) -- ++(-4.5, 0) -- ++(0, -2.35) node[midway, yshift=5pt, sloped] {\textbf 控制总线};
            % 图例
            \node[rectangle, draw, thick, minimum width=0.5cm, minimum height=0.5cm, fill=blue!10] at (-7, 2) {};
            \node at (-6, 2) {CPU};
            \node[rectangle, draw, thick, minimum width=0.5cm, minimum height=0.5cm, fill=red!10] at (-7, 1.5) {};
            \node at (-6, 1.5) {总线译码器};
            \node[rectangle, draw, thick, minimum width=0.5cm, minimum height=0.5cm, fill=green!10] at (-7, 1) {};
            \node at (-6, 1) {内存};
            \draw[thick] (-7.25, 0.5) -- (-6.75, 0.5);
            \node at (-6, 0.5) {总线};
        \end{tikzpicture}
        \caption{计算机内存模型}
        \label{fig:computer_memory}
    \end{figure}



\end{document}